package com.sd.coupon.init;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.sd.coupon.util.GsonUtil;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TbkDgItemCouponGetRequest;
import com.taobao.api.response.TbkDgItemCouponGetResponse;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class InitService {

//    @Resource
//    IItemCouponService itemCouponService;
//
////    @PostConstruct
//    public void init() throws ApiException {
//        TaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest", "25334735", "8468edcb10074eed0c65e0f84698c5aa");
//        TbkDgItemCouponGetRequest req = new TbkDgItemCouponGetRequest();
//        req.setAdzoneId(67776250488L);
//        req.setPlatform(2L);
////        req.setCat("16");
//        req.setPageSize(200L);
//        req.setQ("男装");
//        req.setPageNo(1L);
//        TbkDgItemCouponGetResponse rsp = client.execute(req);
//
//        JsonObject returnData = new JsonParser().parse(rsp.getBody()).getAsJsonObject();
//        JsonArray arr = returnData.get("tbk_dg_item_coupon_get_response").getAsJsonObject().get("results").getAsJsonObject().get("tbk_coupon").getAsJsonArray();
//        List<ItemCoupon> list = new ArrayList<>();
//        for (int i = 0; i < arr.size(); i++) {
//            System.out.println(arr.get(i));
//            ItemCoupon coupon = GsonUtil.parseJsonWithGson(arr.get(i).toString(),ItemCoupon.class);
//            list.add(coupon);
//        }
//        itemCouponService.saveBatch(list);
//    }

}
